package fem2.mesh_and_topology;

/**
 * QuadTree class for 2d indexing
 * 
 * @author hbui
 * 
 */
public class QuadTree extends SpatialTree {

	private double x0, y0;

	public QuadTree(double x0, double y0, double size) {
		this.x0 = x0;
		this.y0 = y0;
		this.size = size;
	}

	@Override
	public boolean isInside(double... p) {
		return p[0] >= x0 && p[0] <= x0 + size && p[1] >= y0 && p[1] <= y0 + size;
	}

	@Override
	public void subdivide() {
		branches = new QuadTree[4];
		branches[0] = new QuadTree(x0, y0, size / 2);
		branches[1] = new QuadTree(x0 + size / 2, y0, size / 2);
		branches[2] = new QuadTree(x0 + size / 2, y0 + size / 2, size / 2);
		branches[3] = new QuadTree(x0, y0 + size / 2, size / 2);
	}

	@Override
	public boolean isIntersected(SpatialCriteria criteria) {
		return criteria.isIntersected(x0, y0, size);
	}

}
